const path = require('path')
const { defineConfig } = require('@vue/cli-service')
const devServerConfig = require('./src/configs/devServer.js')
const appConfig = require('./src/configs/index.js')

const resolve = (dir) => path.join(__dirname, dir)

const publicPath = process.env.VUE_APP_BASE_PATH

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath,
  outputDir: `dist/${publicPath}`,
  lintOnSave: true,
  configureWebpack: {
    devServer: {
      port: devServerConfig.proxyPort,
      proxy: {
        ...(devServerConfig.useProxy && devServerConfig.proxyURL
          ? {
          /** 解决本地测试跨域问题 */
            [`${devServerConfig.proxyPath}`]: {
              target: devServerConfig.proxyURL,
              changeOrigin: true,
              ws: false,
              secure: false,
              pathRewrite: {
                [`^${devServerConfig.proxyPath}`]: '',
              },
            },
          }
          : {}),
      },
    },
    externals: {
      AMap: 'AMap',
    },
  },
  chainWebpack(config) {
    // 设置网站标题
    config.plugin('html').tap((args) => {
      args[0].title = appConfig.appName
      return args
    })
    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]',
      })
      .end()
  },
})
